<template>

  <el-page-header @back="router.go(-1)" title="返回">
    <template #content>
      <span class="text-large font-600 mr-3">{{userId?'编辑':'添加'}}员工</span>
    </template>
  </el-page-header>

  <div class="content">
    <el-form ref="userFormRef"
             style="max-width: 600px"
             :model="userForm"
             :rules="userFormRule"
             :inline="false"
             label-width="100px">

      <el-form-item label="账号:" prop="loginName">
        <el-input v-model="userForm.loginName" placeholder="请输入账号" maxlength="20"/>
      </el-form-item>

      <el-form-item label="姓名:" prop="name">
        <el-input v-model="userForm.name" placeholder="请输入姓名" maxlength="12"/>
      </el-form-item>

      <el-form-item label="手机号:" prop="phone">
        <el-input v-model="userForm.phone" placeholder="请输入手机号" maxlength="11"/>
      </el-form-item>


      <el-form-item>
        <el-button @click="handleSave">保存</el-button>
        <el-button type="primary" @click="handleCancle">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import {reactive, ref} from "vue";
import {useRoute, useRouter} from "vue-router";
import {saveUser} from "@/api/user"

const router = useRouter();
const route = useRoute();

let userFormRef = reactive(null);

let userId = route.query.userId;

let handleSave = () => {
  //判断是否校验通过
  userFormRef.validate((valid) => {
    if (valid) {
      saveUser(userId,userForm).then((resp) => {
        //提示并且返回列表
        router.push({path: '/user/list'});
      });
    }
  });
}

let userForm = reactive({
  name: '',
  loginName: '',
  phone: ''
})

let userFormRule = reactive({
  name: [
    {
      required: true,
      trigger: 'blur'
    }
  ],
  loginName: [
    {
      required: true,
      trigger: 'blur'
    }
  ],
  phone: [{required: true, trigger: 'blur'}]
})
</script>

<style scoped>
.content {
  margin-top: 40px;
}
</style>
